package com.blockchain.securewallet.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.blockchain.securewallet.R;
import com.blockchain.securewallet.WalletApp;
import com.blockchain.securewallet.base.BaseActivity;
import com.blockchain.securewallet.model.db.CoinWallet;
import com.blockchain.securewallet.utils.UIHelper;

import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WalletInfoActivity extends BaseActivity {

    private static final String TAG = "Wallet.Info";

    public static final String KEY_WALLET_ID = "wallet_id";

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_coin_count)
    TextView tvCount;

    @BindView(R.id.cl_wallet_left)
    ConstraintLayout cvLeft;

    private CoinWallet wallet;
    private int id;
    private String value;
    private String format;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_info);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        id = intent.getIntExtra(KEY_WALLET_ID, -1);

        cvLeft.setOnTouchListener(onTouchListener);
    }

    @OnClick(R.id.btn_back)
    void back() {
        finish();
    }

    @OnClick(R.id.cl_wallet_send)
    void send() {
        Intent intent = new Intent();
        intent.setClass(this, SendCoinActivity.class);
        intent.putExtra(ReceiveCoinActivity.KEY_WALLET_ID, id);
        startActivity(intent);
    }

    @OnClick(R.id.cl_wallet_update)
    void update() {
        Intent intent = new Intent();
        intent.setClass(this, QrCodeScanActivity.class);
        intent.putExtra(QrCodeScanActivity.KEY_WALLET_ID, wallet.getId());
        intent.putExtra(QrCodeScanActivity.KEY_MODULE_FROM, QrCodeScanActivity.MODULE_UPDATE_WALLET);
        startActivity(intent);
    }

    @OnClick(R.id.cl_wallet_receive)
    void receive() {
        Intent intent = new Intent();
        intent.setClass(this, ReceiveCoinActivity.class);
        intent.putExtra(ReceiveCoinActivity.KEY_WALLET_ID, id);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        wallet = WalletApp.getInstance().queryCurrrentWallet().get(id);
//        wallet = WalletApp.getInstance().getDaoSession().load(CoinWallet.class, id);
        Log.i(TAG, "wallet " + wallet);
        if (wallet.getType() == CoinWallet.TYPE_BIT_COIN) {
            tvTitle.setText(R.string.bit_coin_name);
            BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).divide(BigDecimal.valueOf(100_000_000));
            value = bigDecimal.toPlainString();
            format = String.format(Locale.getDefault(), "%s %s", value, getResources().getString(R.string.bit_coin_unit));
            //tvCount.setText(format);

        } else if (wallet.getType() == CoinWallet.TYPE_ETH_COIN) {
            tvTitle.setText(R.string.eth_coin_name);
            value = Convert.fromWei(BigDecimal.valueOf(Double.valueOf(wallet.getValue())), Convert.Unit.ETHER).toPlainString();
            format = String.format(Locale.getDefault(), "%s %s", value, getResources().getString(R.string.eth_coin_unit));
            //tvCount.setText(format);

        } else if (wallet.getType() == CoinWallet.TYPE_LTC_COIN) {
            tvTitle.setText(R.string.ltc_coin_name);
//            String v = Convert.fromWei(BigDecimal.valueOf(Double.valueOf(wallet.getValue())), Convert.Unit.ETHER).toPlainString();
            BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).divide(BigDecimal.valueOf(100_000_000));
            value = bigDecimal.toPlainString();
            format = String.format(Locale.getDefault(), "%s %s", value, getResources().getString(R.string.ltc_coin_unit));
            //tvCount.setText(format);
            tvCount.setText(String.format(Locale.getDefault(), "%s%s", value, getResources().getString(R.string.ltc_coin_unit)));
        } else if (wallet.getType() == CoinWallet.TYPE_BCH_COIN){
            tvTitle.setText(R.string.bch_coin_name);
            BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).divide(BigDecimal.valueOf(100_000_000));
            //待补充转单位
            value = bigDecimal.toPlainString();
            format = String.format(Locale.getDefault(), "%s %s", value, getResources().getString(R.string.bch_coin_unit));
            String v = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).toPlainString();
            tvCount.setText(String.format(Locale.getDefault(), "%s%s", v, getResources().getString(R.string.bch_coin_unit)));
        } else if (wallet.getType() == CoinWallet.TYPE_EOS_COIN){
            tvTitle.setText(R.string.eos_coin_name);
//            String v = Convert.fromWei(BigDecimal.valueOf(Double.valueOf(wallet.getValue())), Convert.Unit.ETHER).toPlainString();
            //待补充转单位
            value = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).toPlainString();
            format = String.format(Locale.getDefault(), "%s %s", value, getResources().getString(R.string.eos_coin_unit));
            String v = BigDecimal.valueOf(Double.valueOf(wallet.getValue())).toPlainString();
            tvCount.setText(String.format(Locale.getDefault(), "%s%s", v, getResources().getString(R.string.eos_coin_unit)));
        }

        //高亮显示指定值
        UIHelper.initHighLight(this, format, value, tvCount, R.color.special_text, false);
    }

    void updateByAddress() {
        Intent intent = new Intent();
        intent.setClass(this, QueryAccountQrCodeActivity.class);
        intent.putExtra(QueryAccountQrCodeActivity.KEY_MODULE_FROM, QueryAccountQrCodeActivity.MODULE_WALLET);
        intent.putExtra(QueryAccountQrCodeActivity.KEY_WALLET_ID, wallet.getId());
        startActivity(intent);
    }

    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            TextView tv1 = cvLeft.findViewById(R.id.tv_coin_label);
            TextView tv2 = cvLeft.findViewById(R.id.tv_coin_count);

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    tv1.setBackgroundResource(R.color.btn_bg_pressed);
                    tv2.setBackgroundResource(R.color.btn_bg_pressed);
                    break;
                case MotionEvent.ACTION_UP:
                    tv1.setBackgroundResource(R.color.btn_bg_normal);
                    tv2.setBackgroundResource(R.color.btn_bg_normal);
                    //cvLeft的点击事件
                    //updateByAddress();
                    break;
                case MotionEvent.ACTION_MOVE:
                    //nothing
                    break;
            }
            return true;
        }

    };

}
